package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.DateUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.MessageCenterController;
import com.qire.manhua.dal.service.MessageCenterService;
import com.qire.manhua.databinding.LayoutMessageCenterItemBinding;
import com.qire.manhua.model.enumeration.MessageType;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView.Adapter;

public class MessageCenterViewModel extends SmartRefreshViewModel implements IViewModel {

    private MessageCenterService messageCenterService = new MessageCenterService(this);

    private final ArrayList<PlatformMessageItemViewModel> messageItemList = new ArrayList<>();

    private final SummerRecyclerBindAdapter<PlatformMessageItemViewModel> messageAdapter = new SummerRecyclerBindAdapter<>(messageItemList);

    private int pageNo = 1;

    @Override
    public int getVariableId() {
        return BR.messageCenterViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(MessageCenterController controller) {
        messageAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.messageCenterService = null;
        messageAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    /**
     * 重置页面
     */
    public void resetPage() {
        pageNo = 1;
        clearMessageItemList();
    }

    /**
     * 翻到下一页
     */
    public void nextPage() {
        pageNo++;
    }

    /**
     * 加载页面数据
     */
    public void loadData() {
        resetPage();
        messageCenterService.loadPageData(pageNo);
    }

    /**
     * 加载更多数据
     */
    public void loadMoreData() {
        messageCenterService.loadPageData(pageNo);
    }

    /**
     * 消息列表项数据相关操作
     */
    private void clearMessageItemList() {
//        messageAdapter.notifyItemRangeRemoved(0, messageItemList.size());
        messageItemList.clear();
    }
    public void addMessageItem(int id, String messageTitle, String messageContent, String messageCover,
                               String messageLink, String messageDate, MessageType messageType, boolean haveRead) {
        messageItemList.add(new PlatformMessageItemViewModel(id, messageTitle, messageContent, messageCover,
                messageLink, messageDate, messageType, haveRead));
    }
    public void refreshMessageAdapter() {
//        messageAdapter.notifyItemChanged(0, "full");
        messageAdapter.notifyDataSetChanged();
    }

    /**
     * 标记单个消息已读
     * @param messageId 目标消息ID
     */
    private void markRead(int messageId) {
        messageCenterService.markRead(messageId);
    }

    /**
     * 全部标记已读操作
     */
    public void allMarkRead() {
        boolean existRead = false;
        for(PlatformMessageItemViewModel itemViewModel : messageItemList) {
            if(!itemViewModel.haveRead) {
                itemViewModel.haveRead = true;
                itemViewModel.notifyPropertyChanged(BR.redDotVisible);
                existRead = true;
            }
        }
        if(existRead) {
            messageCenterService.markAllRead();
            refreshAllReadTextColor();
            ToastHelper.showSnackBar("已全部标记已读", Prompt.SUCCESS);
        } else {
            ToastHelper.showToast("没有可清除的消息");
        }
    }

    //endregion #业务数据 end


    //region    #UI操作 begin

    public void refreshUI() {
        refreshMessageAdapter();
        refreshAllReadTextColor();
    }

    public void refreshAllReadTextColor() {
        boolean haveNotRead = false;
        for(PlatformMessageItemViewModel itemViewModel : messageItemList) {
            if(!itemViewModel.haveRead) {
                haveNotRead = true;
                break;
            }
        }
        set("allReadTextColor", haveNotRead ? "#ff9024" : "#aeaeae");
    }

    public void setEnablePushTipsVisible(boolean isHide) {
        set("enablePushTipsVisible", isHide ? View.GONE : View.VISIBLE);
    }

    public void setCommentLastInfo(int commentNumber, String commentContent, String commentDate) {
        boolean isCommentEmpty = commentNumber <= 0;
        boolean isBig = commentNumber > 99;
        set("commentNumberVisible", isCommentEmpty ? View.GONE : View.VISIBLE);
        set("commentNumberText", isBig ? "99+" : String.valueOf(commentNumber));
        set("commentLastDate", DateUtil.yMd.valid(commentDate) ? commentDate : "");
        set("commentLastContent", commentContent);
    }

    public void setLikeLastInfo(int likeNumber, String likeContent, String likeDate) {
        boolean isLikeEmpty = likeNumber <= 0;
        boolean isBig = likeNumber > 99;
        set("likeNumberVisible", isLikeEmpty ? View.GONE : View.VISIBLE);
        set("likeNumberText", isBig ? "99+" : String.valueOf(likeNumber));
        set("likeLastDate", DateUtil.yMd.valid(likeDate) ? likeDate : "");
        set("likeLastContent", likeContent);
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin

    public MutableLiveData<String> getAllReadTextColor() {
        return get("allReadTextColor");
    }

    public MutableLiveData<Integer> getEnablePushTipsVisible() {
        return get("enablePushTipsVisible");
    }

    public MutableLiveData<Integer> getCommentNumberVisible() {
        return get("commentNumberVisible");
    }
    public MutableLiveData<CharSequence> getCommentNumberText() {
        return get("commentNumberText");
    }
    public MutableLiveData<CharSequence> getCommentLastDate() {
        return get("commentLastDate");
    }
    public MutableLiveData<CharSequence> getCommentLastContent() {
        return get("commentLastContent");
    }

    public MutableLiveData<Integer> getLikeNumberVisible() {
        return get("likeNumberVisible");
    }
    public MutableLiveData<CharSequence> getLikeNumberText() {
        return get("likeNumberText");
    }
    public MutableLiveData<CharSequence> getLikeLastDate() {
        return get("likeLastDate");
    }
    public MutableLiveData<CharSequence> getLikeLastContent() {
        return get("likeLastContent");
    }

    public Adapter getMessageAdapter() {
        return messageAdapter;
    }

    //endregion #UI绑定 end

    @AdapterEntityBind(viewRid = R.layout.layout_message_center_item)
    public class PlatformMessageItemViewModel extends BaseObservable implements AutoVariable<LayoutMessageCenterItemBinding> {

        public final int id;
        public final String messageDate;
        public final String messageTitle;
        public final String messageContent;
        public final int messageContentVisible;
        public final String messageCover;
        public final int messageCoverVisible;
        public final String messageLink;
        public final MessageType messageType;

        public boolean haveRead;

        public PlatformMessageItemViewModel(int id, String messageTitle, String messageContent, String messageCover,
                                            String messageLink, String messageDate, MessageType messageType, boolean haveRead) {
            this.id = id;
            this.messageDate = messageDate;
            this.messageTitle = messageTitle;
            this.messageContent = messageContent;
            this.messageContentVisible = StringUtil.isEmpty(messageContent) ? View.GONE : View.VISIBLE;
            this.messageCover = messageCover;
            this.messageCoverVisible = StringUtil.isEmpty(messageCover) ? View.GONE : View.VISIBLE;
            this.messageLink = messageLink;
            this.messageType = messageType;
            this.haveRead = haveRead;
        }

        @Override
        public void bindVariable(LayoutMessageCenterItemBinding viewBind) {
            viewBind.setPlatformMessageItemViewModel(this);
        }

        public void readMessage() {
            if(!haveRead) {
                haveRead = true;
                markRead(id);
                refreshAllReadTextColor();
                notifyPropertyChanged(BR.redDotVisible);
            }
        }

        @Bindable
        public int getRedDotVisible() {
            return haveRead ? View.GONE : View.VISIBLE;
        }

    }

}
